import Vue from 'vue';

const size = (value, precise = 1, mode) => {
    if (!value) return '-';
    value = +value;
    const units = ['K', 'M', 'G'];
    if (!units.includes(mode)) {
        if (value / Math.pow(1024, 3) >= 1) mode = 'G';
        else if (value / Math.pow(1024, 2) < 1) mode = 'K';
        else mode = 'M';
    }
    const i = units.findIndex((v) => mode === v);
    const ratio = Math.pow(1024, i + 1);
    return (value / ratio).toFixed(precise) + mode;
};

//上传文件 size
Vue.filter('size', size);
